package com.example.idachuappone;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class BaseActivity extends FragmentActivity {
	public static List<Activity> activitys;
	public Button btn_back;
	public Button btn_index;
	public FragmentManager fm;
	public FragmentTransaction ft;
	public LinearLayout ll_back;
	public TextView tv_title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base);
		if (activitys == null) {
			activitys = new ArrayList();
		}
		activitys.add(this);
		this.fm = getSupportFragmentManager();
	}

	public void twoNone() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.tv_title = (TextView) findViewById(R.id.tv_title);
		this.ll_back = (LinearLayout) findViewById(R.id.ll_back);
		topClick();
	}

	public void two() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setVisibility(0);
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setVisibility(0);
		this.tv_title = (TextView) findViewById(R.id.tv_title);
		this.ll_back = (LinearLayout) findViewById(R.id.ll_back);
		topClick();
	}

	public void leftNone() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setVisibility(0);
		this.tv_title = (TextView) findViewById(R.id.tv_title);
		this.ll_back = (LinearLayout) findViewById(R.id.ll_back);
		topClick();
	}

	public void rightNone() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setVisibility(0);
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.tv_title = (TextView) findViewById(R.id.tv_title);
		this.ll_back = (LinearLayout) findViewById(R.id.ll_back);
		topClick();
	}

	public void topClick() {
		this.btn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				BaseActivity.this.finish();
			}
		});
		this.ll_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				BaseActivity.this.btn_back.performClick();
			}
		});
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (ev.getAction() == 0) {
			View v = getCurrentFocus();
			if (isShouldHideInput(v, ev)) {
				hideSoftInput(v.getWindowToken());
			}
		}
		return super.dispatchTouchEvent(ev);
	}

	private boolean isShouldHideInput(View v, MotionEvent event) {
		if (v == null || !(v instanceof EditText)) {
			return false;
		}
		int[] l = new int[2];
		v.getLocationInWindow(l);
		int left = l[0];
		int top = l[1];
		return event.getX() <= (left) || event.getX() >= (left + v.getWidth()) || event.getY() <= (top)
				|| event.getY() >= (top + v.getHeight());
	}

	private void hideSoftInput(IBinder token) {
		if (token != null) {
			((InputMethodManager) getSystemService("input_method")).hideSoftInputFromWindow(token,
					BitmapCacheManagementTask.MESSAGE_FLUSH);
		}
	}

	protected void exitApp(Context context) {
		for (Activity ac : activitys) {
			if (ac != null) {
				ac.finish();
			}
		}
	}
}
